'use strict'

const { ApiController } = require('jax')

class Controller extends ApiController {
  get Model() {
    return this.ctx.model.UserActivity
  }

  // 匹配查询
  get searchKey() {
    return ['isOpen', 'status', 'userId', 'storeId']
  }

  // 模糊查询
  get likeKey() {
    return ['verifyUserName', 'name', 'mobile', 'storeName', 'remark']
  }

  // 查询前
  async beforeIndex() {
    const { ctx, config } = this
    const { SYSTEM_TYPE_STORE, ACTIVITY_STATUS_SUCCESS } = config.constant
    const { query } = ctx
    const { helper } = ctx
    const filter = {}

    if (this.admin.systemType == SYSTEM_TYPE_STORE) filter.storeId = this.admin.sourceId

    if (query.from == 'comment') filter.status = ACTIVITY_STATUS_SUCCESS

    // 开始时间
    if (query.activityStateDate && query.activityStateDate != '')
      filter.activityDate = {
        $gte: new Date(query.activityStateDate + ' 00:00:00'),
      }

    // 结束时间
    if (query.activityEndDate && query.activityEndDate != '')
      filter.activityDate = {
        ...filter.activityDate,
        $lte: new Date(query.activityEndDate + ' 23:59:59'),
      }

    if (query.activityDate) {
      filter.activityDate = {
        $gte: new Date(query.activityDate + ' 00:00:00'),
        $lte: new Date(query.activityDate + ' 23:59:59'),
      }
    }

    return {
      filter,
    }
  }

  // 详情查询前
  async beforeShow(filter) {
    const { config } = this
    const { SYSTEM_TYPE_STORE } = config.constant

    if (this.admin.systemType == SYSTEM_TYPE_STORE) filter.storeId = this.admin.sourceId
  }

  // 审核
  async audit() {
    const { ctx, config } = this
    const {
      SYSTEM_TYPE_STORE,
      ACTIVITY_STATUS_AUDIT_WAIT,
      ACTIVITY_STATUS_VERIFY_WAIT,
      ACTIVITY_STATUS_AUDIT_REFUSE,
    } = config.constant
    const { helper } = ctx
    const { user } = ctx.state
    const { body } = ctx.request

    if (this.admin.systemType != SYSTEM_TYPE_STORE) throw '非法操作'

    helper.validate(body, {
      required: ['activityId', 'auditStatus'],
    })

    const userActivity = await ctx.model.UserActivity.findOne({
      _id: body.activityId,
      storeId: this.admin.sourceId,
    })

    if (!userActivity) throw '获取预约信息失败'
    if (userActivity.status != ACTIVITY_STATUS_AUDIT_WAIT) throw '当前状态不允许审核'

    // 审核通过
    if (body.auditStatus == 1) {
      userActivity.status = ACTIVITY_STATUS_VERIFY_WAIT
    } else {
      if (!body.auditRemark) throw '请填写拒绝原因'
      userActivity.auditRemark = body.auditRemark
      userActivity.status = ACTIVITY_STATUS_AUDIT_REFUSE
    }

    await userActivity.save()

    ctx.body = {
      code: 0,
      msg: 'success',
    }
  }

  // 取消
  async cancel() {
    const { ctx, config } = this
    const {
      SYSTEM_TYPE_STORE,
      ACTIVITY_STATUS_AUDIT_WAIT,
      ACTIVITY_STATUS_VERIFY_WAIT,
      ACTIVITY_STATUS_CANCEL,
    } = config.constant
    const { helper } = ctx
    const { user } = ctx.state
    const { body } = ctx.request

    if (this.admin.systemType != SYSTEM_TYPE_STORE) throw '非法操作'

    helper.validate(body, {
      required: ['activityId'],
    })

    const userActivity = await ctx.model.UserActivity.findOne({
      _id: body.activityId,
      storeId: this.admin.sourceId,
    })

    if (!userActivity) throw '获取预约信息失败'
    if (![ACTIVITY_STATUS_AUDIT_WAIT, ACTIVITY_STATUS_VERIFY_WAIT].includes(userActivity.status))
      throw '当前状态不允许取消'

    userActivity.status = ACTIVITY_STATUS_CANCEL
    await userActivity.save()

    // 将活动改为可以预约
    await ctx.model.Activity.findOneAndUpdate(
      {
        _id: userActivity.activityId,
      },
      {
        status: 0,
      }
    )

    ctx.body = {
      code: 0,
      msg: 'success',
    }
  }

  // 导出
  async exportConfig() {
    return [
      { name: '姓名', key: 'name' },
      { name: '手机号', key: 'mobile' },
      { name: '预约日期', key: 'activityDate' },
    ]
  }
}

module.exports = Controller
